/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.cakerun.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import top.continew.admin.cakerun.mapper.BoardMapper;
import top.continew.admin.cakerun.model.req.GeneralReq;
import top.continew.admin.cakerun.model.resp.GeneralCountResp;
import top.continew.admin.cakerun.model.resp.GeneralDailyResp;
import top.continew.admin.cakerun.model.resp.GeneralProjectResp;
import top.continew.admin.cakerun.model.resp.page.CakerunPageResp;
import top.continew.admin.cakerun.service.BoardService;
import top.continew.starter.extension.crud.model.query.PageQuery;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BoardServiceImpl implements BoardService {
    private final BoardMapper boardMapper;

    @Override
    public CakerunPageResp<GeneralCountResp, GeneralCountResp> generalCountPage(GeneralReq req, PageQuery pageQuery) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        IPage<GeneralCountResp> general = boardMapper.generalCountPage(new Page<>(pageQuery.getPage(), pageQuery
            .getSize()), startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds()) ? null : req.getAccountIds());
        GeneralCountResp generalCountResp = this.generalCountSummary(req);
        return CakerunPageResp.build(generalCountResp, general);
    }

    @Override
    public GeneralCountResp generalCountSummary(GeneralReq req) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        return boardMapper.generalCountSummary(startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
            ? null
            : req.getAccountIds());
    }

    @Override
    public List<GeneralCountResp> generalCount(GeneralReq req) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        return boardMapper.generalCount(startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
            ? null
            : req.getAccountIds());
    }

    @Override
    public CakerunPageResp<GeneralDailyResp, GeneralDailyResp> generalDailyPage(GeneralReq req, PageQuery pageQuery) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        IPage<GeneralDailyResp> general = this.boardMapper.generalDailyPage(new Page<>(pageQuery.getPage(), pageQuery
            .getSize()), startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds()) ? null : req.getAccountIds());
        GeneralDailyResp generalDailyResp = this.generalDailyRespSummary(req);
        return CakerunPageResp.build(generalDailyResp, general);
    }

    private GeneralDailyResp generalDailyRespSummary(GeneralReq req) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        return boardMapper.generalDailyRespSummary(startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
            ? null
            : req.getAccountIds());
    }

    @Override
    public List<GeneralDailyResp> generalDaily(GeneralReq req) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        return this.boardMapper.generalDaily(startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
            ? null
            : req.getAccountIds());
    }

    @Override
    public CakerunPageResp<GeneralProjectResp, GeneralProjectResp> generalProjectPage(GeneralReq req,
                                                                                      PageQuery pageQuery) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        IPage<GeneralProjectResp> general = this.boardMapper.generalProjectPage(new Page<>(pageQuery
            .getPage(), pageQuery.getSize()), startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
                ? null
                : req.getAccountIds());
        GeneralProjectResp generalProjectResp = this.generalProjectSummary(req);
        return CakerunPageResp.build(generalProjectResp, general);
    }

    @Override
    public List<GeneralProjectResp> generalProject(GeneralReq req) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        return this.boardMapper.generalProject(startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
            ? null
            : req.getAccountIds());
    }

    @Override
    public GeneralProjectResp generalProjectSummary(GeneralReq req) {
        String startDate = null;
        String endDate = null;
        if (req.getBusinessTime() != null && req.getBusinessTime().size() >= 2) {
            startDate = req.getBusinessTime().get(0);
            endDate = req.getBusinessTime().get(1);
        }
        return boardMapper.generalProjectSummary(startDate, endDate, CollectionUtils.isEmpty(req.getAccountIds())
            ? null
            : req.getAccountIds());
    }
}
